<?php
    class chat_Ajax extends Ajax
    {
        public function insertChat()
        {
            core::checkSession();
            $oDatabase = new database();
            $aData = array(
                'chatUser_ser' => $_SESSION['username'],
                'chatContent' =>$_POST['content'],
                'chatUser_client' =>$_POST['userClent'],
                'time' =>time(),
                'status' =>1,
                'display' =>1
            );
            $result = $oDatabase->add('chat',$aData); 
            $time = date('H:i' ,time());
            echo $time;
        }
        public function displayMessage()
        {
            $message ='';
            $oDatabase = new database();
            core::checkSession();
            $squeryCount='select * from chat where (chatUser_ser="'.$_SESSION['username'].'" and 
            chatUser_client ="'.$_POST['userclient'].'") or (chatUser_client="'.$_SESSION['username'].'" and 
            chatUser_ser="'.$_POST['userclient'].'" ) ORDER BY chat_id DESC ';
            $aCount = $oDatabase->getListObject($squeryCount,true);
            if((int)$_POST['param']==1)
            {
                $this->updatestatusmessage($_POST['userclient'],$_SESSION['username']);
                $squery ='select * from chat where (chatUser_ser="'.$_SESSION['username'].'" and 
                chatUser_client ="'.$_POST['userclient'].'") or (chatUser_client="'.$_SESSION['username'].'" and 
                chatUser_ser="'.$_POST['userclient'].'" ) ORDER BY chat_id DESC LIMIT '.$_POST['limit'];
                // print($squery);
                $aChat = $oDatabase->getListObject($squery,true);
                $message ='';
                if(count($aChat))
                {
                    foreach($aChat as $key => $mes)
                    {
                        $color ='';
                        if($mes->chatUser_ser == $_SESSION['username'])
                            $color = 'blue';
                        else
                            $color = 'red';
                        $mes ='<span style="color :'.$color.';font-size: 13px; margin-left :5px;">'.$mes->chatUser_ser.'</span><br><div style="margin-left :5px;"><span style="font-size: 13px;">'.$mes->chatContent.'<p style="float:right; margin-right:10px;color: gray;">'.date('H:i' , $mes->time).'</p></span></div><br><br>';
                        $message = $mes.$message;

                    }
                    $sShowmore ='';
                    if(count($aCount)> count($aChat))
                    {
                        $sShowmore = '<br>------------------------------------------------------<br>
                        <p id="bt_showmore" style="margin-left:100px;color:blue;cursor: pointer;"> show more message</p>
                        ------------------------------------------------------<br>';
                    }
                    $message = $sShowmore .$message ;
                }
            }
            else
            {
                $aCount = array();
                $mess='select * from chat where chatUser_ser="'.$_POST['userclient'].'" and 
                chatUser_client ="'.$_SESSION['username'].'" and status = 1 ORDER BY chat_id DESC ';
                $aCount = $oDatabase->getListObject($mess,true);
                if(count($aCount))
                {
                    foreach($aCount as $mes)
                    {
                        $this->updatedisPlay($mes);
                        if($mes->display !=0)
                        {
                            $mess ='<span style="color :red ;font-size: 13px; margin-left :5px;">'.$mes->chatUser_ser.'</span><div style="margin-left :5px;"><span style="font-size: 13px;"><p style="float:right; margin-right:10px;color: gray;">'.$mes->chatContent.date('H:i' , $mes->time).'</p></span></div>';
                            $message = $mess.$message;
                        }
                        if($_POST['scroll'] !='')
                        {
                            $this->updatestatusmessage($_POST['userclient'],$_SESSION['username']);
                        }
                    }
                }
            }
            echo $message;
        }
        function displayMessage_sub()
        {
            $message ='';
            $oDatabase = new database();
            core::checkSession();
            $squeryCount='select * from chat where (chatUser_ser="'.$_SESSION['username'].'" and 
            chatUser_client ="'.$_POST['userclient'].'") or (chatUser_client="'.$_SESSION['username'].'" and 
            chatUser_ser="'.$_POST['userclient'].'" ) ORDER BY chat_id DESC ';
            $aCount = $oDatabase->getListObject($squeryCount,true);
            // if((int)$_POST['param']==1)
            // {
            $this->updatestatusmessage($_POST['userclient'],$_SESSION['username']);
            $squery ='select * from chat where (chatUser_ser="'.$_SESSION['username'].'" and 
            chatUser_client ="'.$_POST['userclient'].'") or (chatUser_client="'.$_SESSION['username'].'" and 
            chatUser_ser="'.$_POST['userclient'].'" ) ORDER BY chat_id DESC LIMIT '.$_POST['limit'];
            // print($squery);
            $aChat = $oDatabase->getListObject($squery,true);
            $message ='';
            if(count($aChat))
            {
                foreach($aChat as $key => $mes)
                {
                    $color ='';
                    if($mes->chatUser_ser == $_SESSION['username'])
                        $color = 'my_message';
                    else
                        $color = 'friend_message';
                    $data ='<li class="'.$color.'" title="'.date('H:i' , $mes->time).'">'.$mes->chatContent.'</li>';  
                    // $mes ='<span style="color :'.$color.';font-size: 13px; margin-left :5px;">'.$mes->chatUser_ser.'</span><br><div style="margin-left :5px;"><span style="font-size: 13px;">'.$mes->chatContent.'<p style="float:right; margin-right:10px;color: gray;">'.date('H:i' , $mes->time).'</p></span></div><br><br>';
                    $message = $data.$message;
                }
                $sShowmore ='';
                if(count($aCount)> count($aChat))
                {
                    $sShowmore = '<li id="li_'.$_POST['userclient'].'" class="my_message_more">SHOW MORE MESSAGE</li>';
                }
                $message = $sShowmore .$message ;
            }
            //}

            /* else
            {
            $aCount = array();
            $mess='select * from chat where chatUser_ser="'.$_POST['userclient'].'" and 
            chatUser_client ="'.$_SESSION['username'].'" and status = 1 ORDER BY chat_id DESC ';
            $aCount = $oDatabase->getListObject($mess,true);
            if(count($aCount))
            {
            foreach($aCount as $mes)
            {
            $this->updatedisPlay($mes);
            if($mes->display !=0)
            {
            $mess ='<span style="color :red ;font-size: 13px; margin-left :5px;">'.$mes->chatUser_ser.'</span><div style="margin-left :5px;"><span style="font-size: 13px;"><p style="float:right; margin-right:10px;color: gray;">'.$mes->chatContent.date('H:i' , $mes->time).'</p></span></div>';
            $message = $mess.$message;
            }
            if($_POST['scroll'] !='')
            {
            $this->updatestatusmessage($_POST['userclient'],$_SESSION['username']);
            }
            }
            }
            }*/
            echo $message;   
        }
        function chatMessage()
        {
            $oDatabase = new database();
            core::checkSession();
            $this->onlinelineUser();
            $data ='';
            $mess='select * from chat where chatUser_client="'.$_SESSION['username'].'" and status = 1 ORDER BY chat_id ASC ';
            $aMessage = $oDatabase->getListObject($mess,true);
            $arrayMessage = array();
            if(count($aMessage))
            {
                foreach($aMessage as $key => $mes)
                {
                    $array = array();
                    array_push($array,$mes->chatUser_ser);
                    array_push($array,$mes ->chatContent);
                    array_push($array,date('H:i' , $mes->time));
                    array_push($array,$mes ->chat_id);
                }
                array_push($arrayMessage,$array);// fill content chat
            }
            $arrayOnline = array();
            $arrayOnline = $this->getUser_sub();// Get list user online
            $countNotification = $this->updateNotification();// count notification
            $poupNotification = $this->loadNofication();
            $process = array(
                'numberNotification' => $countNotification,
                'message' => $arrayMessage,
                'onlineUser' => $arrayOnline,
                'poupNotification' => $poupNotification,
            );
            echo json_encode($process);;
        }
        function updateMessageAjax()
        {
            $oDatabase = new database();
            $aCondition = array(
                'chat_id = "'.$_POST['id'] => '"',
            );
            $aData = array(
                'status' => 0    
            );

            $result = $oDatabase->update('chat',$aData, $aCondition);
        }
        function updatestatusmessage($client,$user)
        {
            $oDatabase = new database();
            $aCount = array();
            $mess='select * from chat where chatUser_ser="'.$client.'" and 
            chatUser_client ="'.$user.'" and status = 1  ORDER BY chat_id DESC ';
            $aCount = $oDatabase->getListObject($mess,true);
            if(count($aCount))
            {
                foreach($aCount as $mes)
                {
                    $aCondition = array(
                        'chat_id = "'.$mes->chat_id => '"',

                    );
                    $aData = array(
                        'status' => 0    
                    );

                    $result = $oDatabase->update('chat',$aData, $aCondition);
                }
            }

        }
        function updatedisPlay($mes)
        {
            $oDatabase = new database();
            $aCondition = array(
                'chat_id = "'.$mes->chat_id => '"',
            );
            $aData = array(
                'display' => 0    
            );
            $result = $oDatabase->update('chat',$aData, $aCondition);
        }
        function getUser()
        {
            core::checkSession();
            if(isset($_SESSION['username']))
            {
                $aUser = array();
                $oDatabase = new database();
                $squery ='select * from user where user_id !="'.$_SESSION['username'].'"';
                $aUser = $oDatabase->getListObject($squery);
                $data='';
                if(count($aUser))
                {
                    foreach($aUser as $user)
                    {
                        $notice= '';
                        $countnotice= '';
                        $querynotice ='select * from chat where chatUser_ser="'.$user->user_id.'" and chatUser_client ="'.$_SESSION['username'].'" and status = 1 ORDER BY chat_id DESC';
                        $anotice = $oDatabase->getListObject($querynotice);
                        if(count($anotice))
                        {
                            $notice ='<img style=" padding-left: 20px;" src="'.URL::getLink("image/message").'">';
                            $countnotice = count($anotice);
                        }
                        $status ='';
                        if($user->online == 1)
                            $status = 'online';
                        else
                            $status = 'offline';
                        $data = $data.'<div class="id_chat" id="'.$user->user_id.'" style="height: 50px; width: 100%; background-color: #FFFFCC;border:1px solid #333;">
                        <label style=" padding-left: 10px; float: left; line-height: 50px;font-weight: bold"><img style=" margin-right: 5px;" src="'.URL::getLink("image/$status").'">'.$user->user_id.'
                        '.$notice.$countnotice.'</label></div>' ;
                    }
                }
                echo $data;
            }
        }
        function getUser_sub()
        {
            $arrayonline = array();
            core::checkSession();
            if(isset($_SESSION['username']))
            {
                $aUser = array();
                $oDatabase = new database();
                $squery ='select * from user where user_id !="'.$_SESSION['username'].'"';
                $aUser = $oDatabase->getListObject($squery);
                if(count($aUser))
                {
                    foreach($aUser as $user)
                    {
                        $array = array();
                        $notice= '';
                        $countnotice= '';
                        $querynotice ='select * from chat where chatUser_ser="'.$user->user_id.'" and chatUser_client ="'.$_SESSION['username'].'" and status = 1 ORDER BY chat_id DESC';
                        $anotice = $oDatabase->getListObject($querynotice);
                        if(count($anotice))
                        {
                            $notice ='<img style=" padding-left: 20px; height=20px;width:20px;" src="'.URL::getLink("image/message").'">';
                            $countnotice = count($anotice);
                        }
                        $status ='';
                        if($user->online == 1)
                            $status = 'online';
                        else
                            $status = 'offline';
                        // $data = $data.'<li val="'.$user->user_id.'"><img style=" margin-right: 5px;" src="'.URL::getLink("image/$status").'">'.$user->user_id.$notice.$countnotice.'</li>' ;
                        $data = '<li val="'.$user->user_id.'"><img style=" margin-right: 5px;" src="'.URL::getLink("image/$status").'">'.$user->user_id.$notice.$countnotice.'</li>' ;
                        array_push($array,$data);
                        $this->activeUser($user->user_id);
                        $tb_limit ='<input id="tb_'.$user->user_id.'" value = "10" style ="width:97%"></input>';
                        array_push($array,$tb_limit);
                        array_push($arrayonline,$array);
                        //$data .=';;'.$tb_limit.'--';
                    }
                }
                return $arrayonline;
            }  
        }
        function activeUser($user)
        {
            $oDatabase = new database();
            $aCondition = array(
                'user_id = "'.$user => '"',
            );
            $aData = array(
                'active' => 0    
            );
            $result = $oDatabase->update('user',$aData, $aCondition);
        }
        function onlinelineUser()
        {
            core::checkSession();
            if(isset($_SESSION['username']))
            {
                $oDatabase = new database();
                $aCondition = array(
                    'user_id = "'.$_SESSION['username'] => '"',
                );
                $aData = array(
                    'online' => 1    
                );
                $result = $oDatabase->update('user',$aData, $aCondition);
            }
        }

        public function updateNotification()
        {
            core::checkSession();
            $oDatabase = new database();
            $data = 0;
            if(isset($_SESSION['username']))
            {
                $query ='select *from nofication where user_to="'.$_SESSION['username'].'" and status = 1 ORDER BY nofication_id ASC';
                $aNofication = $oDatabase->getListObject($query);
                if(count($aNofication))
                {
                    $data =count($aNofication);
                }
            }
            return $data;
        }

        public function loadNofication()
        {
            core::checkSession();
            $oDatabase = new database();
            $data = array();
            //  $data = $data.'<div class ="nofication_title"><lable id="id_title_nofication">Thông Báo</lable></div>';
            if(isset($_SESSION['username']))
            {
                $query ='select *from nofication where user_to="'.$_SESSION['username'].'" and status = 1 and poup = 1 ORDER BY nofication_id ASC';
                $aNofication = $oDatabase->getListObject($query);
                if(count($aNofication))
                {
                    $count = 0;
                    foreach($aNofication as $user)
                    {
                        $bottom = $count*90 +50;
                        $snofication =$this->printNotification($user,$bottom);
                        // $data.=$snofication;
                        array_push($data,$snofication);
                        $count++;
                        $aCondition = array(
                            'nofication_id = "'.$user->nofication_id => '"',
                        );
                        $aData = array(
                            'poup' => 0    
                        );
                        $result = $oDatabase->update('nofication',$aData, $aCondition);
                    }
                }

            }
            return $data;
        }
        public function printNotification($user, $bottom)
        {
            $icon ='';
            $type = $user->type;
            if($type == 1)
            {
                $icon ='pro_noti.png';
            }
            else
            {
                if($type == 2)
                {
                    $icon ='ment.png';
                }
                else
                {
                    $icon ='like.png';
                }
            }
            $data = '<div class ="poup_notification" style="bottom:'.$bottom.'px;">';
            $data.=' <div class="poup_nofication">' ;
            $data.='<div style="margin-bottom: 10px;">';
            $data.='<div style="float: left; width: 10%; margin-left : 5px;">';
            $data.='<img style=" height: 50px; width: 50px;" src="'.URL::getLink('upload/'.$user->avata).'"></div>';
            $data.='<div style="float:left;padding-left: 20px; width: 80%;">';
            $data.='<div class="arer_comment" style ="line-height :0px;">';
            $data.='<label hidden="" class="id_comment_product">'.$user->product.'</label>';
            $data.='<label style="color: #DF1B23; font-weight: bold;">'.$user->user_send.' </label>';
            $data.='<label style="font: 200 13px/1.5 Georgia, Times New Roman, serif;">'.$user->content.' </label><br> ';
            $data.='<label style="font: 200 11px/1.5 Georgia, Times New Roman, serif; color: #3581e0;"><img style="" src="'.URL::getLink('image/'.$icon).'"> <label style="font-size: 12px; color: #A6A6A6; ">Lúc : '.date('H:i Y-m-d' ,$user->time).'</label></label>';
            $data.='</div></div>';
            $data.='</div><div class="add_reply" style="clear: both;"></div></div></div>';
            return $data;
        }
    }
?>
